<?php

namespace Main\Model;

// 控制器基类
use FC\Model\BaseModel;

class comments extends BaseModel
{
	
    function __construct()
    {
		// 数据库配置类型,默认为Mysql
		//$this->db_type = 'Mysql';		
		// 数据库配置链接名称,默认为default
		//$this->db_config_name = 'default';
		
        $this->rules = [
            // 类方法验证
            'content'   => ['必填',[$this, 'check_content']],
        ];		
		
		// 执行父类
        parent::__construct();

        //echo $this->table;		
    }
	
	public function check_content($content = ''){
		
		$content = strip_tags($content);
		
		if(\Tools\keywordcheck($content)){
			return $content;
		}
		return null;
	}
	
    /**
     * 获取数据(重写这个函数,让他支持联表查询)
     *
     * @return array
     */
    public function query(array $datas = [])
    {
        $page   = (int)  isset($datas['page']) ? $datas['page'] : 1;
        $limit  = (int) isset($datas['limit']) ? $datas['limit'] : 10;
        $offset = (int) isset($datas['offset']) ? $datas['offset'] : 0;
		$status = (int) isset($datas['status']) ? $datas['status'] : 0;
        $skey   = isset($datas['skey']) ? $datas['skey'] : '';
        $sname  = isset($datas['sname']) ? $datas['sname'] : '';
        unset($datas['sname']);
        unset($datas['skey']);
        unset($datas['page']);
		unset($datas['status']);
        // 表名
        $table = $this->table;
        // 检测变量值
        if (!empty($skey) && !empty($sname)) {
            // 搜索这个值
            $datas[$skey] = ['LOCATE', $sname];
        }
        $where  = $this->filterValue($datas);
		if($status!=0){
		    $where['a.status']= $status;
		}
        // 排序方式
        $order  = (isset($datas['order']) && $datas['order'] === 'desc') ? 'desc'  : 'asc';
        // 排序字段 
        $sortby = isset($datas['sortby']) ? $datas['sortby'] : $this->primary;
        // 获取数量
        $number   = $this->db->name($table,'a')->join('commentator','a.uid = u.uid','left','u')->where($where)->number();
        // 获取分页值
        list($total, $offset2) = $this->page($number, $page, $limit);
        // 看看从哪里开始
        if ($offset != 0) {
            $offset2 = $offset;
        }
        $limit = $offset2 . ',' . $limit;
        $res   = $this->db->getid('a.*,u.uid,u.email,u.homeurl,u.nickname,u.imgurl,u2.nickname as hf_name')->name($table,'a')->join('commentator','a.uid = u.uid','left','u')->join('commentator','a.hf_uid = u2.uid','left','u2')->where($where)->order($sortby, $order)->limit($limit)->fetchall();
		//echo $this->db->lastsql();
		//die();		
        if ($res) {
            return ['data' => $res, 'page' => ['page' => $page, 'number' => $number, 'total' => $total, 'offset' => $offset]];
        } else {
            return null;
        }
    }	
}